package com.suo.enroll.utils;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

public class InfoUtils {

//TODO
 /*   public static List<String> getUniversity(String ocr) {
        String[] words = null;
        try {
            words = getWords(ocr);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        List<String> universityList = preGetUniversity(words);
        return universityList;
    }
*/
    public static String[] getWords(String ocr) throws JSONException {
        // 解析 JSON 数据
        JSONObject jsonObject = new JSONObject(ocr);
        JSONArray wordsArray = jsonObject.getJSONArray("words_result");

        // 创建存放 words 的字符串数组
        String[] words = new String[wordsArray.length()];

        // 提取 "words" 字段
        for (int i = 0; i < wordsArray.length(); i++) {
            JSONObject wordObj = wordsArray.getJSONObject(i);
            words[i] = wordObj.getString("words");
        }

        return words;
    }

  /*  public static List<String> preGetUniversity(String[] words) {
        // 存放含有"大学"的字符串的列表
        List<String> universityList = new ArrayList<>();

        // 遍历数组并提取含有"大学"的字符串
        for (String word : words) {
            if (word.contains("大学")) {
                universityList.add(word);
            }
        }

        return universityList;
    }

    public static String getSubject(String[] words) {
        for (String word : words) {
            if (word.contains("085410")) {
                return "085410(人工智能)";
            }
            if (word.contains("085404")) {
                return "085404(体系结构)";
            }
        }
        return null;
    }

    public static boolean isHIAS(String[] words) {
        for (String word : words) {
            if (word.contains("杭州高等研究院")) {
                return true;
            }
        }
        return false;
    }

    public static boolean isZN(String[] words) {
        for (String word : words) {
            if (word.contains("01")) {
                if (word.contains("人工智能")|| word.contains("智能体系结构")) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getUndergraduate(List<String> universitys) {
        for (String university : universitys) {
            if (!university.contains("中国科学院大学")) {
                return university;
            }
        }
        return null;
    }*/

    //尝试优化

    public static class InfoResult {
        private List<String> universityList;
        private String subject;
        private boolean isHIAS;
        private boolean isZN;
        private boolean isTime;
        private String PrintTime;


        public InfoResult() {
            this.universityList = new ArrayList<>();
            this.subject = null;
            this.isHIAS = false;
            this.isZN = false;
            this.isTime = false;
            this.PrintTime = null;
        }

        public List<String> getUniversityList() {
            return universityList;
        }

        public String getSubject() {
            return subject;
        }

        public boolean isHIAS() {
            return isHIAS;
        }

        public boolean isZN() {
            return isZN;
        }

        public boolean isTime() {
            return isTime;
        }
        public String getPrintTime() {
            return PrintTime;
        }

        public void addUniversity(String university) {
            this.universityList.add(university);
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public void setHIAS(boolean isHIAS) {
            this.isHIAS = isHIAS;
        }

        public void setZN(boolean isZN) {
            this.isZN = isZN;
        }

        public void setTime(boolean isTime) {
            this.isTime = isTime;
        }
        public void setPrintTime(String PrintTime) {
            this.PrintTime = PrintTime;
        }
    }
    public static InfoResult extractInfo(String[] words) {
        InfoResult result = new InfoResult();

        for (String word : words) {
            if (word.contains("大学")) {
                result.addUniversity(word);
            }
            if (word.contains("085410")) {
                result.setSubject("085410(人工智能)");
            }
            if (word.contains("085404")) {
                result.setSubject("085404(体系结构)");
            }
            if (word.contains("杭州高等研究院")) {
                result.setHIAS(true);
            }
            if (word.contains("01")) {
                if (word.contains("人工智能") || word.contains("智能体系结构")) {
                    result.setZN(true);
                }
            }
            if (word.contains("打印时间") && word.contains("2024")){
                result.setTime(true);
                result.setPrintTime(word);
            }
        }

        return result;
    }


    public static List<String> preGetUniversity(InfoResult result) {
        return result.getUniversityList();
    }
    public static String getSubject(InfoResult result) {
        return result.getSubject();
    }
    public static boolean isHIAS(InfoResult result) {
        return result.isHIAS();
    }
    public static boolean isZN(InfoResult result) {
        return result.isZN();
    }
    public static boolean isTime(InfoResult result) {
        return result.isTime();
    }
    public static String getPrintTime(InfoResult result) {
        return result.getPrintTime();
    }

    public static String getUndergraduate(List<String> universitys) {
        String undergraduate = null;
        for (String university : universitys) {
            if (!university.contains("中国科学院大学")) {
                undergraduate = university;
            }
        }
        return undergraduate;
    }

}


